package com.peanut.controller;

import com.alibaba.fastjson.JSON;

import com.peanut.common.Result;
import com.peanut.domain.bo.ForumCommentBO;
import com.peanut.domain.po.ForumComment;
import com.peanut.domain.po.TabAdmin;
import com.peanut.em.ResultEnum;
import com.peanut.service.ForumCommentService;
import com.peanut.service.ForumTitleService;
import com.peanut.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * mongoDB 车评评论接口
 */
@RestController
@RequestMapping("forumComment")
@CrossOrigin
@Slf4j
@Api("车评评论接口")
public class ForumCommentController {

    @Autowired
    private ForumCommentService commentService;

    @Autowired
    private ForumTitleService forumTitleService;

    //封装验证登录态方法
    private TabAdmin getTabAdmin(String token) {
        TabAdmin tabAdmin = JSON.parseObject(new JwtUtil().parseJwt(token).getSubject(),TabAdmin.class);
        return tabAdmin;
    }

    //增加/发表评论，需要验证登录态，评论人为添加人
    @PostMapping("insertComment")
    @ApiOperation("增加/发表一级评论，此处添加forumId为帖子ID")
    public Result insertComment(ForumCommentBO forumCommentDTO,
                                @RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                ForumCommentBO bo = new ForumCommentBO();
                //DTO转BO传到service层处理
                BeanUtils.copyProperties(forumCommentDTO,bo);
                boolean b = commentService.insertComment(bo,tabAdmin);
                return Result.OK().setData(b);
            }
            else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            //增加失败
            return Result.ERROR();
        }
    }

    //增加/发表子级评论，需要验证登录态，评论人为添加人
    @PostMapping("insertChildComment")
    @ApiOperation("增加/发表子级评论，此处添加fatherCommentId为一级评论ID")
    public Result insertChildComment(ForumCommentBO forumCommentDTO,
                                @RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                ForumCommentBO bo = new ForumCommentBO();
                //DTO转BO传到service层处理
                BeanUtils.copyProperties(forumCommentDTO,bo);
                boolean b = commentService.insertChildComment(bo,tabAdmin);
                return Result.OK().setData(b);
            }
            else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            //增加失败
            return Result.ERROR();
        }
    }

    //删除评论，同时删除该评论下的所有子评论
    @DeleteMapping("deleteComment/{id}")
    @ApiOperation("删除评论，同时删除该评论下的所有子评论")
    @ApiImplicitParam(name = "id",value = "评论主键",required = true,paramType = "path")
    public Result deleteComment(@PathVariable("id") String id,@RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                ForumComment comment = commentService.selectById(id);
                //返回删除数量
                Long b = commentService.deleteComment(id);
                //减去帖子的删除数
                boolean i = forumTitleService.updateCommentNumber(comment.getForumId(),b);
                return Result.OK();
            } else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    //点赞评论
    @PutMapping("likeComment/{id}")
    @ApiOperation("点赞评论")
    @ApiImplicitParam(name = "id",value = "评论主键",required = true,paramType = "path")
    public Result likeComment(@PathVariable("id") String id,@RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                commentService.likeComment(id);
                return Result.OK();
            } else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    //修改评论
    @PutMapping("updateComment/{id}")
    @ApiOperation("修改评论")
    @ApiImplicitParam(name = "id",value = "评论主键",required = true,paramType = "path")
    public Result updateComment(@PathVariable("id") String id,
                                @RequestParam(required = true) String forumComment,
                                @RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                commentService.updateComment(id,forumComment);
                return Result.OK();
            } else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
